package com.dxb.yomall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dxb.yomall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dxb.yomall.product.entity.AttrGroupEntity;
import com.dxb.yomall.product.service.AttrGroupService;
import com.dxb.yomall.product.vo.AttrGroupWithAttrsVO;
import com.dxb.common.utils.PageUtils;
import com.dxb.common.utils.R;



/**
 * 
 *
 * @author daixinbao
 * @email 1065808217@qq.com
 * @date 2021-01-05 22:26:07
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类下所有分组及关联的所有属性
     * @return
    */
     @GetMapping("/{catelogId}/withattr")
     public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
         List<AttrGroupWithAttrsVO> vos =attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
         return R.ok().put("data",vos);
     }
    /**
     * 查询属性分组
     * @param attrGroupId 属性分组ID
     * @return 属性分组信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        // 查找三级分类的完整路径 [level1Id, level2Id, level3Id]
        Long[] catelogPath = categoryService.findCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }
    /**
     * 根据三级分类ID查询父类下的属性分组
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    /*@RequestMapping("/info/{attrGroupId}")
        public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }*/

    /**
     * 保存
     */
    @RequestMapping("/save")
        public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
        public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
